package org.opencv.ImgSDK;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;
import java.nio.ByteBuffer;

public class TrackSDK {
    public static Intent data;
    public static boolean hasPermission = false;
    public static boolean hasStart = false;
    public static ImageReader imageReader;
    private static volatile boolean mHasCheckAllScreen;
    private static volatile boolean mIsAllScreenDevice;
    public static OnDestroyRecordListener mOnDestroyRecordListener;
    public static OnStrartRecordListener mOnStrartRecordListener;
    private static final TrackSDK ourInstance = new TrackSDK();
    public static MediaProjection projection;
    public static MediaProjectionManager projectionManager;
    public static int resultCode;
    public static Bitmap targetBitmap;
    private Context mContext;
    public OnCallDestroyListener mOnCallDestroyListener;

    public interface OnCallDestroyListener {
        void result(boolean z);
    }

    public interface OnDestroyRecordListener {
        void result(boolean z, String str);
    }

    public interface OnStrartRecordListener {
        void result(boolean z, String str);
    }

    public static TrackSDK getInstance() {
        return ourInstance;
    }

    private TrackSDK() {
    }

    public void startRecording(Context context, OnStrartRecordListener onStrartRecordListener) {
        this.mContext = context;
        mOnStrartRecordListener = onStrartRecordListener;
        hasStart = false;
        if (!isRecording()) {
            Intent intent = new Intent(context, TrackLibActivity.class);
            intent.addFlags(268435456);
            context.startActivity(intent);
        }
    }

    public void destroyRecording(OnDestroyRecordListener onDestroyRecordListener) {
        OnCallDestroyListener onCallDestroyListener = this.mOnCallDestroyListener;
        if (onCallDestroyListener != null) {
            onCallDestroyListener.result(true);
        }
        hasStart = false;
        mOnDestroyRecordListener = onDestroyRecordListener;
    }

    public boolean isRecording() {
        return imageReader != null;
    }

    public Bitmap getBitMap() {
        Log.d("TrackLibActivity", "开始获取bitmap");
        try {
            Image acquireNextImage = imageReader.acquireNextImage();
            if (acquireNextImage != null) {
                Log.d("TrackLibActivity", "image不为空");
                int width = acquireNextImage.getWidth();
                int height = acquireNextImage.getHeight();
                Image.Plane[] planes = acquireNextImage.getPlanes();
                ByteBuffer buffer = planes[0].getBuffer();
                int pixelStride = planes[0].getPixelStride();
                Bitmap createBitmap = Bitmap.createBitmap(((planes[0].getRowStride() - (pixelStride * width)) / pixelStride) + width, height, Bitmap.Config.ARGB_8888);
                createBitmap.copyPixelsFromBuffer(buffer);
                acquireNextImage.close();
                reflect(imageReader);
                if (isScreenOriatationPortrait(this.mContext)) {
                    int width2 = createBitmap.getWidth() * createBitmap.getHeight();
                    int[] iArr = new int[width2];
                    createBitmap.getPixels(iArr, 0, createBitmap.getWidth(), 0, 0, createBitmap.getWidth(), 1);
                    int i = 0;
                    while (true) {
                        if (i >= width2) {
                            i = 0;
                            break;
                        } else if (iArr[i] != 0) {
                            break;
                        } else {
                            i++;
                        }
                    }
                    try {
                        createBitmap = Bitmap.createBitmap(createBitmap, i, 0, width - (i * 2), height);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    int width3 = createBitmap.getWidth() * createBitmap.getHeight();
                    int[] iArr2 = new int[width3];
                    createBitmap.getPixels(iArr2, 0, 1, 0, 0, 1, createBitmap.getHeight());
                    int i2 = 0;
                    while (true) {
                        if (i2 >= width3) {
                            i2 = 0;
                            break;
                        } else if (iArr2[i2] != 0) {
                            break;
                        } else {
                            i2++;
                        }
                    }
                    int i3 = height - (i2 * 2);
                    try {
                        createBitmap = Bitmap.createBitmap(createBitmap, 0, i2, width, i3);
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                    if (getInstance().isAllScreenDevice(this.mContext)) {
                        return Bitmap.createBitmap(createBitmap, getStatusBarHeight(this.mContext), 0, width - getStatusBarHeight(this.mContext), i3);
                    }
                }
                return createBitmap;
            }
            Log.d("TrackLibActivity", "image为空");
            return null;
        } catch (Exception e3) {
            e3.printStackTrace();
            return null;
        }
    }

    public void setOnCallDestroyListener(OnCallDestroyListener onCallDestroyListener) {
        this.mOnCallDestroyListener = onCallDestroyListener;
    }

    public boolean isAllScreenDevice(Context context) {
        float f;
        int i;
        if (mHasCheckAllScreen) {
            return mIsAllScreenDevice;
        }
        mHasCheckAllScreen = true;
        mIsAllScreenDevice = false;
        if (Build.VERSION.SDK_INT < 21) {
            return false;
        }
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            Display defaultDisplay = windowManager.getDefaultDisplay();
            Point point = new Point();
            defaultDisplay.getRealSize(point);
            if (point.x < point.y) {
                f = (float) point.x;
                i = point.y;
            } else {
                f = (float) point.y;
                i = point.x;
            }
            if (((float) i) / f >= 1.97f) {
                mIsAllScreenDevice = true;
            }
        }
        return mIsAllScreenDevice;
    }

    public void reflect(ImageReader imageReader2) {
        try {
            imageReader2.getClass().getDeclaredMethod("discardFreeBuffers", new Class[0]).invoke(imageReader2, new Object[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isScreenOriatationPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == 1;
    }

    public static int getStatusBarHeight(Context context) {
        int identifier = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            return context.getResources().getDimensionPixelSize(identifier);
        }
        return (int) TypedValue.applyDimension(1, (float) 24, Resources.getSystem().getDisplayMetrics());
    }
}
